/* * jQuery-Plugin "Simple clearField"
 * 
 * @version: 1.1.1, 14.11.2009
 * 
 * @author: Andres Vidal
 *          code@andresvidal.com
 *          http://www.andresvidal.com
 * 
 * @example: $('selector').clearField("Default Text");
 * @example: $('selector').clearField("Default Text", { blurClass: 'myBlurredClass', activeClass: 'myActiveClass', stopSubmit: true });
 * 
 */

(function($) {

	$.fn.clearField = function(text, options) {
		// define config(defaults) and override with settings, if available - by extending the config settings, we don't modify the argument
		var settings = jQuery.extend({
			blurClass: "default",
			activeClass: '',
			stopSubmit: true
		}, options);
		
		// do the rest of the plugin, using settings
		// loop each element
		this.each(function() {
			// element-specific code here
			var el = $(this);
			var cvalue = el.val();
			var parentForm = el.parents('form');			

			// onload check if value is present, else add settings.text and settings.blurClass
			if(cvalue == "" || cvalue == text) {
				el.addClass(settings.blurClass).val(text);
			}
			
			// Set focus action
			el.focus(function() {
				var val = jQuery.trim( el.val() );
				if ( val == text ) {
					el.removeClass(settings.blurClass).addClass(settings.activeClass).val('');
				}
			});
			
			// Set blur action
			el.blur(function() {										
				var val = jQuery.trim( el.val() );
				if( val == "") {
					el.removeClass(settings.activeClass).addClass(settings.blurClass).val(text);
				}			
			});
			
			//check on form submit if default text is changed
			parentForm.submit(function(){
				var val = jQuery.trim( el.val() );
				if( val == "" || val == text && settings.stopSubmit == true) {
					return false;
				}
			});
			
		});
		
		return this;
	};
	
})(jQuery);